﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Tomahawk.Runtime.Localization;
using Microsoft.Xna.Framework.Media;

namespace Tomahawk.Runtime.LoadingScreens
{
    public sealed class UIVideo : UIControl
    {
        #region Serializable Properties
        [XmlAttribute("FlipHorizontally")]
        public bool FlipHorizontally = false;

        [XmlAttribute("FlipVertically")]
        public bool FlipVertically = false;

        [XmlElement("Color")]
        public Color Color = Color.White;

        [XmlElement("Video")]
        public string VideoName = "";

        [XmlAttribute("Autoplay")]
        public bool Autoplay = true;

        [XmlAttribute("Mute")]
        public bool Mute = false;

        [XmlAttribute("Loop")]
        public bool Loop = false;
        #endregion

        #region Properties
        private string localizedVideoName = null;
        private Video video = null;
        private VideoPlayer videoPlayer = null;

        public Video Video
        {
            get { return video; }
        }

        public VideoPlayer Player
        {
            get { return videoPlayer; }
        }
        #endregion

        #region Initialization
        public UIVideo()
        {
            this.videoPlayer = new VideoPlayer();
            this.videoPlayer.IsLooped = Loop;
            this.videoPlayer.IsMuted = Mute;
        }
        #endregion

        #region Loading
        internal override void Load(ContentManager contentManager, LocalizationManager localizationManager)
        {
            if (Localized)
            {
                localizationManager.GetLocalizedString(VideoName, out localizedVideoName);
            }
            else
            {
                localizedVideoName = VideoName;
            }

            video = contentManager.Load<Video>(localizedVideoName);

            if (Autoplay)
            {
                videoPlayer.Play(this.video);
            }

            base.Load(contentManager, localizationManager);
        }
        #endregion

        #region Localization
        internal override void Localize(ContentManager contentManager, LocalizationManager localizationManager)
        {
            if (Localized)
            {
                string oldVideoName = localizedVideoName;
                if (string.Compare(oldVideoName, localizedVideoName, true) != 0)
                {
                    bool autoplay = (videoPlayer.State == MediaState.Playing);
                    bool mute = videoPlayer.IsMuted;
                    bool loop = videoPlayer.IsLooped;

                    videoPlayer.Dispose();

                    video = contentManager.Load<Video>(localizedVideoName);

                    videoPlayer = new VideoPlayer();
                    videoPlayer.IsLooped = loop;
                    videoPlayer.IsMuted = mute;

                    if (autoplay)
                    {
                        videoPlayer.Play(video);
                    }
                }
            }

            base.Localize(contentManager, localizationManager);
        }
        #endregion

        #region Render
        internal override void Render(UICanvas canvas, SpriteBatch spriteBatch)
        {
            if (!videoPlayer.IsDisposed)
            {
                Color imageColor = Color.White;
                imageColor.R = (byte)((canvas.overlayColor.R * Color.R) / 255.0f);
                imageColor.G = (byte)((canvas.overlayColor.G * Color.G) / 255.0f);
                imageColor.B = (byte)((canvas.overlayColor.B * Color.B) / 255.0f);
                imageColor.A = (byte)((canvas.overlayColor.A * Color.A) / 255.0f);

                SpriteEffects effect = SpriteEffects.None;
                effect |= FlipHorizontally ? SpriteEffects.FlipHorizontally : SpriteEffects.None;
                effect |= FlipVertically ? SpriteEffects.FlipVertically : SpriteEffects.None;

                canvas.ApplyVirtualCanvas(ref RenderRectangle, ref ScreenRectangle);
                spriteBatch.Draw(videoPlayer.GetTexture(), ScreenRectangle, null, imageColor, RenderRotation, RenderOrigin, effect, this.Depth);
            }
        }
        #endregion
    }
}
